var spiderGoodsDao = module.exports;
var goodsDao = require('./goods.js');
var spiderVideoDao = require('./spider_video_url.js');
var request = require('request');
var commonUtil = require('../util/common');
var aliApiHelper = require('../util/aliApiHelper');
var biz2hour4hot = 180;
var cheerio = require('cheerio');
var DBFactory = require('../conn/DBFactory');

var tag4fqcat = ['全部', '女装', '男装', '内衣', '美妆', '配饰', '鞋品', '箱包', '儿童', '母婴', '居家', '美食', '数码', '家电', '其他', '文体', '车品'];
var tag4cid =   ['全部', '女装', '母婴', '美妆', '居家', '鞋包服饰', '美食', '文体车品', '数码家电', '男装', '内衣', '其他'];
var cid2fqcat = {0:0,     1:1,    2:9,    3:4,    4:10,   5:14,       6:11,   7:14,       8:12,       9:2,    10:3,   11:14};

function getGoodsInfoFromAlimama(goodsList, cb) {
    var auctionIds = [];
    var result = {};
    goodsList.forEach(function (g) {
        auctionIds.push(g.auctionId);
        result[g.auctionId] = {dbDat:g};
    })
    aliApiHelper.getGoodsInfoSimple(auctionIds, function (err, rspAli) {
        if(err){
            cb(err);
            return;
        }
        if(rspAli == '' || rspAli == undefined) {
            console.log(rspAli);
            return;
        }
        rspAli.forEach(function (g) {
            g.avail = 1;
            var dat = result[g.num_iid];
            if(dat) dat.aliDat = g;
        });
        cb(null, result);
    });
}

function onSaveGoods(err, result) {
    if(err) console.log(err);
}

function saveGatherGoods(goods, source, cb) {
    if(goods.length > 0) {
        getGoodsInfoFromAlimama(goods, function (err, rspAli) {
            if(err){
                cb('采集商品后，从API取店铺信息：' + err);
                return;
            }
            var goodsAvail = [];
            goods.forEach(function (g) {
                var ali = rspAli[g.auctionId];
                if(!ali || !ali.aliDat) return;
                g.shopTitle = ali.aliDat.nick;
                g.sellerId = ali.aliDat.seller_id;
                goodsAvail.push(g);
            })
            goodsDao.batchCreate(goodsAvail, 0, {source: source}, function (err) {
                if(err){
                    cb('采集商品后，添加失败！' );
                    return;
                }
                console.log('采集成功！');
                cb(null);
            });
        });
    }
    // goodsDao.batchCreate(goods, 0, {source: source}, function (err) {
    //     if(err){
    //         cb('采集商品后，添加失败！' );
    //         return;
    //     }
    //     console.log('采集成功！');
    //     cb(null);
    // });
}

function spiderDtkGoods(page, timely) {
    request({url:'http://api.dataoke.com/index.php?r=Port/index&type=total&appkey=169foxqf9m&v=2&page=' + page}, function (error, response, body) {

        if(error || response.statusCode != 200 || !body){
            
            console.log('大淘客获取失败！');
            return;
        }
        try{
            var rsp = JSON.parse(body);
            if(!rsp || !rsp.data ||!rsp.result){
                goodsDao.updateTag('鞋品', '鞋');
                goodsDao.updateTag('箱包', '包包');
                goodsDao.updateTag('配饰', '饰品');
                goodsDao.delInvalidGoods();
                spiderVideoDao.autoSpiderVideo({page: 1}, function(){});
                console.log('大淘客获取失败！');
                return;
            }
            var result = rsp.result;
            var goods = [];
            var auctionIds = [];
            var minGoodsKeyDTK = 999999999;
            var maxGoodsKeyDTK = 0;
            result.forEach(function (g, i) {
                auctionIds.push(g.GoodsID);
                if(maxGoodsKeyDTK < g.ID) maxGoodsKeyDTK = g.ID;
                if(minGoodsKeyDTK > g.ID) minGoodsKeyDTK = g.ID;
                var good = {
                    source:'大淘客',
                    auctionId:g.GoodsID,
                    pictUrl:g.Pic,
                    title:g.Title,
                    reservePrice:g.Org_Price,
                    zkPrice:g.Org_Price,
                    biz30day:g.Sales_num,
                    sellerId:g.SellerID,
                    adTitle:g.Introduce,
                    userType:g.IsTmall,
                    isTMall:g.IsTmall,
                    hiddenAmount:g.Quan_price,
                    hiddenActivityId:g.Quan_id,
                    hiddenEndTime:g.Quan_time,
                    hiddenLeftCount:g.Quan_surplus,
                    hiddenTotalCount:g.Quan_receive + g.Quan_receive,
                    goodShortTitle:g.D_title,
                    includeDxjh:1,
                    extActivityId:g.Quan_id
                };

                if(isNaN(g.Quan_condition)){
                    good.hiddenInfo = g.Quan_condition;
                }
                else{
                    good.hiddenStartFee = parseFloat(g.Quan_condition);
                }
                good.fqCat = cid2fqcat[g.Cid];
                good.tag = tag4cid[g.Cid];
                var commJihua = parseFloat(g.Commission_jihua);
                var commQQHD = parseFloat(g.Commission_queqiao);
                if(commJihua >= commQQHD){
                    good.realTkRate = commJihua;
                    good.tkRate = 0;
                    good.eventRate = 0;
                    good.maxTkRate = good.realTkRate;
                    good.tkType = '定向';
                }
                else{
                    good.realTkRate = commQQHD;
                    good.tkRate = 0;
                    good.maxTkRate = 0;
                    good.eventRate = good.realTkRate;
                    good.tkType = '鹊桥';
                }
                var realPrice = g.Price;
                good.tkCommFee = realPrice * good.realTkRate / 100;
                if(!good.activityType || good.activityType == '普通活动'){
                    good.activityType = '宝贝精选';
                }
                if(g.planlink){
                    good.planlink = commonUtil.GetQueryString(g.planlink, 'me');
                }
                if(good.realTkRate >= 35 && good.tkCommFee > 8){
                    good.isHighCoupon = 1;
                }else{
                    good.isHighCoupon = 0;
                }
                var realPrice = g.Price;
                if(realPrice < 10) good.isCheap = 1; else good.isCheap = 0;
                if(realPrice <= good.hiddenAmount) good.isHighScore = 1; else good.isHighScore = 0;
                if(parseInt(g.Sales_num) * parseFloat(g.Price) > 100000)  good.isHot = 1;

                if( (parseFloat(good.zkPrice) - parseFloat(good.hiddenAmount)) > 0 ) {
                    goods.push(good);
                }

            });
            var arr = {};
            arr.auctionIdList = auctionIds;
            goodsDao.isExist(arr, function(err, data) {
                if(err) {
                    console.log(err);
                    console.log('采集大淘客后，检查重复商品失败！');
                    return;
                }
                goods = commonUtil.duplicateRemoval(data, goods);
                console.log(goods.length);
                if(timely ==  true) {
                    if(goods.length == 0) {
                        return;
                    } else {
                        console.log(22);
                        do{
                            if(goods.length > 40){
                                saveGatherGoods(goods.splice(0, 40), '大淘客', onSaveGoods);
                            }
                            else{
                                saveGatherGoods(goods.splice(0, goods.length), '大淘客', onSaveGoods);
                            }
                        } while (goods.length > 0);
                        ++page;
                        setTimeout(function(){
                            spiderDtkGoods(page, timely);
                        }, 2500);
                    }
                } else {
                    do{
                        if(goods.length > 40){
                            saveGatherGoods(goods.splice(0, 40), '大淘客', onSaveGoods);
                        }
                        else{
                            saveGatherGoods(goods.splice(0, goods.length), '大淘客', onSaveGoods);
                        }
                    } while (goods.length > 0);
                    if(goods.length == 0) {
                        ++page;
                        console.log(page);
                        setTimeout(function(){
                            spiderDtkGoods(page);
                        }, 2500);
                    }
                }
            });
        }
        catch (e) {
            console.log(e);
        }
    })
}

function spiderHdkGoods(page) {
    request({url:'http://www.haodanku.com/?json=true&api=list&p=' + page}, function (error, response, body) {

        if(error || response.statusCode != 200 || !body){
            console.log('好单库获取失败！');
            return;
        }
        try{
            if(!body || body.length < 2 || body.substr(0, 1) != '['){
                console.log('好单库获取失败');
                return;
            }
            var rsp = JSON.parse(body);
            var goods = [];
            var auctionIds = [];
            rsp.forEach(function (g, i) {
                auctionIds.push(g.itemid);
                var good = {
                    source:'好单库',
                    auctionId:g.itemid,
                    pictUrl:g.itempic,
                    shopTitle:g.shopname,
                    title:g.itemtitle,
                    tkCommFee:g.tkmoney,
                    reservePrice:g.itemprice,
                    zkPrice:g.itemprice,
                    biz30day:g.itemsale,
                    fqCat:(g.hasOwnProperty('fqcat')) ? g.fqcat : 14,
                    sellerId:g.userid,
                    tkType:g.tktype,
                    realTkRate:parseFloat(g.tkrates),
                    isBrand:g.is_brand,
                    hiddenAmount:g.couponmoney,
                    couponLeftCount:g.couponsurplus,
                    couponTotalCount:g.couponnum,
                    couponInfo:g.couponexplain,
                    adTitle:g.itemdesc,
                    includeDxjh:1,
                    activityType:g.activity_type,
                    goodShortTitle:g.itemshorttitle
                };
                if(!good.sellerId){
                    good.sellerId = g.shopid;
                }
                if(!good.shopTitle || good.shopTitle.length < 2){
                    good.shopTitle = g.sellernick;
                }
                if(!good.activityType || good.activityType == '普通活动'){
                    good.activityType = '宝贝精选';
                }
                if(!good.shopTitle || good.shopTitle.length < 2){
                    good.shopTitle = '淘宝/天猫店';
                }
                if(g.couponurl){
                    good.hiddenActivityId = commonUtil.GetQueryString(g.couponurl, 'activityId');
                    if(!good.hiddenActivityId){
                        good.hiddenActivityId = commonUtil.GetQueryString(g.couponurl, 'activity_id');
                    }
                    good.extActivityId = good.hiddenActivityId;
                }
                if(g.planlink){
                    good.planlink = commonUtil.GetQueryString(g.planlink, 'me');
                }
                if(good.realTkRate >= 35 && good.tkCommFee > 8){
                    good.isHighCoupon = 1;
                }else{
                    good.isHighCoupon = 0;
                }
                var realPrice = good.zkPrice - good.hiddenAmount;
                if(g.itemsale2 * realPrice> biz2hour4hot) good.isHot = 1;
                if(realPrice < 10) good.isCheap = 1; else good.isCheap = 0;
                if(realPrice <= good.hiddenAmount) good.isHighScore = 1; else good.isHighScore = 0;
                good.includeDxjh = 1;

                good.tag = tag4fqcat[good.fqCat];
                good.userType = (g.shoptype == ('B')) ? 1 : 0;
                good.tkRate = (good.tkType.indexOf('通用') >= 0) ? good.realTkRate : 0;
                good.maxTkRate = (good.tkType.indexOf('定向') >= 0) ? good.realTkRate : 0;
                good.eventRate = (good.tkType.indexOf('鹊桥') >= 0) ? good.realTkRate : 0;

                if(g.couponstarttime) {
                    good.hiddenStartTime = commonUtil.formatDate(new Date(parseInt(g.couponstarttime) * 1000));
                }
                if(g.couponendtime) {
                    good.hiddenEndTime = commonUtil.formatDate(new Date(parseInt(g.couponendtime) * 1000));
                }
                if(g.couponexplain) {
                    good.hiddenInfo = g.couponexplain;
                }
                if(g.couponsurplus) {
                    good.hiddenLeftCount = g.couponsurplus;
                }
                if(g.couponnum) {
                    good.hiddenTotalCount = g.couponnum;
                }
                if( (good.zkPrice - good.hiddenAmount) > 0 ) {
                    goods.push(good);
                }

            });
            var arr = {};
            arr.auctionIdList = auctionIds;
            goodsDao.isExist(arr, function(err, data) {
                if(err) {
                    console.log('采集商品后，检查重复商品失败！');
                    return;
                }
                goods = commonUtil.duplicateRemoval(data, goods );
                do{
                    if(goods.length > 40){
                        saveGatherGoods(goods.splice(0, 40), '好单库', onSaveGoods);
                    }
                    else{
                        saveGatherGoods(goods.splice(0, goods.length), '好单库', onSaveGoods);
                    }
                }while(goods.length > 0);
                if(goods.length == 0) {
                    ++page;
                    console.log(page);
                    setTimeout(function(){
                        spiderHdkGoods(page);
                    }, 4000);
                }
            });
        }
        catch (e) {
            console.log(e);
        }
    })
}

var mtkTag = {1:'女装',2:'男装',3:'内衣',4:'鞋包',5:'居家',6:'母婴',7:'美妆',8:'食品',9:'数码',10:'文体',11:'配饰',12:'家电',13:'其他'};

function spiderMtkGoods(page) {
    request({url:'http://www.miaodake.com/api/getitems?page=' + page}, function (error, response, body) {
        if(error || response.statusCode != 200 || !body){
            console.log('喵淘客获取失败！');
            return;
        }
        try{
            var rsp = JSON.parse(body);
            if(!rsp || !rsp.data){
                console.log('喵淘客获取失败！');
                return;
            }
            var result = rsp.data;
            var auctionIds = [];
            var goods = [];
            result.forEach(function(g, i) {
                auctionIds.push(g.itemid);
                var good = {
                    source: '喵淘客',
                    auctionId: g.itemid,
                    pictUrl: g.pic_url,
                    shopTitle: g.s_title,
                    title: g.title,
                    tkCommFee: (parseFloat(g.qhprice) * parseFloat(g.rate) / 100).toFixed(2) ,
                    reservePrice: g.price,
                    zkPrice: g.price,
                    biz30day: g.sales,
                    sellerId: g.sellerid,
                    realTkRate: parseFloat(g.rate),
                    hiddenAmount: g.quan_price,
                    couponInfo: '优惠券满'+ g.quan_man + '可用！',
                    couponAmount: g.quan_price,
                    hiddenInfo: '优惠券满'+ g.quan_man + '可用！',
                    userType: g.user_type,
                    hiddenActivityId: g.activityid,
                    hiddenStartTime: g.quan_starttime,
                    hiddenEndTime: g.quan_endtime,
                    hiddenStartFee: g.quan_man,
                    tkRate: g.rate,
                    adTitle: g.descs,
                    isCheap: parseInt(g.qhprice) < 10 ? 1 : 0,
                    isHot: parseInt(g.sales) > 5000 ? 1 : 0,
                    tag: mtkTag[g.cid],
                    goodShortTitle:g.s_title,
                    extActivityId: g.activityid
                };
                goods.push(good);
            });
            var arr = {};
            arr.auctionIdList = auctionIds;
            goodsDao.isExist(arr, function(err, data) {
                if(err) {
                    console.log('采集喵淘客后，检查重复商品失败！');
                    return;
                }
                goods = commonUtil.duplicateRemoval(data, goods);
                console.log(goods.length);
                do{
                    if(goods.length > 40){
                        saveGatherGoods(goods.splice(0, 40), '喵淘客', onSaveGoods);
                    }
                    else{
                        saveGatherGoods(goods.splice(0, goods.length), '喵淘客', onSaveGoods);
                    }
                }while(goods.length > 0);
                if(goods.length == 0) {
                    ++page;
                    console.log(page);
                    setTimeout(function(){
                        spiderMtkGoods(page);
                    }, 4000);
                }
            });
        }
        catch (e) {
            console.log(e);
        }
    });
}

var ysdTag = {1:'女装',2:'男装',3:'内衣',4:'母婴',5:'包包',6:'母婴',341:'美妆',8:'鞋品',9:'美食',10:'文体',13:'配饰',7:'居家',11:'家电', 12:'其他'};

function spiderYsdGoods(page) {
    request({url:'http://www.yishoudan.com/rmsp/index/pname/cid/aname/cid/p/'+ page +'/volume_hour/desc.html?json=1'}, function (error, response, body) {
        if(error || response.statusCode != 200 || !body){
            console.log('一手单获取失败！');
            return;
        }
        try{
            var rsp = JSON.parse(body);
            if(!rsp){
                console.log('一手单获取失败！');
                return;
            }
            var result = rsp;
            var auctionIds = [];
            var goods = [];
            result.forEach(function(g, i){
                auctionIds.push(g.num_iid);
                var good = {
                    source: '一手单',
                    auctionId: g.num_iid,
                    pictUrl: g.pic_url,
                    shopTitle: g.oldtitle,
                    title: g.title,
                    tkCommFee: (parseFloat(g.coupon_quan_price) * parseFloat(g.commission_rate) / 10000).toFixed(2) ,
                    reservePrice: g.coupon_price,
                    zkPrice: g.coupon_price,
                    biz30day: g.volume,
                    sellerId: g.sellerId,
                    realTkRate: parseFloat(parseInt(g.commission_rate)/100),
                    hiddenAmount: g.quan,
                    couponAmount: g.quan,
                    hiddenActivityId: g.activity_id,
                    hiddenStartTime: g.quan_starttime,
                    tkRate: parseFloat(parseInt(g.commission_rate)/100),
                    adTitle: g.intro,
                    isCheap: parseInt(g.coupon_quan_price) < 10 ? 1 : 0,
                    isHot: parseInt(g.volume) > 5000 ? 1 : 0,
                    tag: ysdTag[g.cid],
                    goodShortTitle:g.oldtitle,
                    extActivityId: g.activity_id
                };
                if(g.coupon_start_time) {
                    good.hiddenStartTime = commonUtil.formatDate(new Date(parseInt(g.coupon_start_time) * 1000));
                }
                if(g.coupon_end_time) {
                    good.hiddenEndTime = commonUtil.formatDate(new Date(parseInt(g.coupon_end_time) * 1000));
                }
                goods.push(good);
            });
            var arr = {};
            arr.auctionIdList = auctionIds;
            goodsDao.isExist(arr, function(err, data) {
                if(err) {
                    console.log('采集一手单后，检查重复商品失败！');
                    return;
                }
                goods = commonUtil.duplicateRemoval(data, goods);
                console.log(goods.length);
                do{
                    if(goods.length > 40){
                        saveGatherGoods(goods.splice(0, 40), '一手单', onSaveGoods);
                    }
                    else{
                        saveGatherGoods(goods.splice(0, goods.length), '一手单', onSaveGoods);
                    }
                }while(goods.length > 0);
                if(goods.length == 0) {
                    ++page;
                    console.log(page);
                    setTimeout(function(){
                        spiderYsdGoods(page);
                    }, 2000);
                }
            });
        }
        catch (e) {
            console.log(e);
        }
    });
}

var tkjdTag = {1:'女装',2:'男装',3:'内衣',4:'母婴',5:'美妆',6:'居家',7:'鞋包配饰',8:'美食',9:'文体车品',10:'数码家电',11:'运动户外', 12:'其他'};

function spiderTkjdGoods(page) {
    request({url:'http://api.tkjidi.com/getGoodsLink?appkey=4aea809e020ed9c437b5a35b8303c4fc&type=www_lingquan&page=' + page}, function (error, response, body) {
        if(error || response.statusCode != 200 || !body){
            console.log('淘客基地获取失败！');
            return;
        }
        try{
            var rsp = JSON.parse(body);
            if(!rsp || rsp.status != '200'){
                console.log('淘客基地获取失败！');
                return;
            }
            var result = rsp.data;
            var auctionIds = [];
            var goods = [];
            result.forEach(function(g, i){
                auctionIds.push(g.goods_id);
                var good = {
                    source: '淘客基地',
                    auctionId: g.goods_id,
                    pictUrl: g.pic,
                    shopTitle: g.goods_name,
                    auctionUrl: g.goods_url,
                    title: g.goods_name,
                    tkCommFee: (parseFloat(g.price_after_coupons) * parseFloat(g.rate) / 100).toFixed(2) ,
                    reservePrice: g.price,
                    zkPrice: g.price,
                    biz30day: g.sales,
                    sellerId: g.seller_id,
                    realTkRate: parseFloat(g.rate),
                    hiddenAmount: g.price_coupons,
                    couponAmount: g.price_coupons,
                    tkType: g.commission_type_name,
                    userType: g.src,
                    hiddenActivityId: g.quan_id,
                    hiddenEndTime: g.quan_expired_time,
                    tkRate: g.rate,
                    adTitle: g.quan_guid_content,
                    isCheap: parseInt(g.price_after_coupons) < 10 ? 1 : 0,
                    isHot: parseInt(g.sales) > 5000 ? 1 : 0,
                    tag: tkjdTag[g.cate_id],
                    extActivityId: g.quan_id
                };
                goods.push(good);
            });
            var arr = {};
            arr.auctionIdList = auctionIds;
            goodsDao.isExist(arr, function(err, data) {
                if(err) {
                    console.log('采集喵淘客后，检查重复商品失败！');
                    return;
                }
                goods = commonUtil.duplicateRemoval(data, goods);
                console.log(goods.length);
                do{
                    if(goods.length > 40){
                        saveGatherGoods(goods.splice(0, 40), '喵淘客', onSaveGoods);
                    }
                    else{
                        saveGatherGoods(goods.splice(0, goods.length), '喵淘客', onSaveGoods);
                    }
                }while(goods.length > 0);
                if(goods.length == 0) {
                    ++page;
                    console.log(page);
                    setTimeout(function(){
                        spiderTkjdGoods(page);
                    }, 4000);
                }
            });
        }
        catch (e) {
            console.log(e);
        }
    });
}

var qtkTag = {0:'全部', 2:'母婴', 3:'美妆', 4:'居家', 5:'鞋包配饰', 6:'美食',7:'文体', 8:'家电数码;',9:'其他',10:'女装',11:'内衣',12:'男装'};
var commissionType = {1:'定向', 2:'高佣',3:'通用', 4:'营销计划'};
function spiderQtkGoods(page) {
    request({url:'http://openapi.qingtaoke.com/qingsoulist?app_key=FLP4unYk&v=1.0&page=' + page}, function (error, response, body) {
        if(error || response.statusCode != 200 || !body){
            console.log('轻淘客获取失败！');
            return;
        }
        try{
            var rsp = JSON.parse(body);
            if(!rsp || !rsp.data || !rsp.data.list){
                console.log('轻淘客获取失败！');
                return;
            }
            var result = rsp.data.list;
            var auctionIds = [];
            var goods = [];
            result.forEach(function(g, i) {
                auctionIds.push(g.goods_id);
                var good = {
                    source: '轻淘客',
                    auctionId: g.goods_id,
                    pictUrl: g.goods_pic,
                    shopTitle: g.goods_short_title,
                    title: g.goods_title,
                    tkCommFee: (parseFloat(g.commission) * (parseFloat(g.goods_price) - parseInt(g.coupon_price)) / 100).toFixed(2) ,
                    reservePrice: g.goods_price,
                    zkPrice: g.goods_price,
                    biz30day: g.goods_sales,
                    sellerId: g.seller_id,
                    realTkRate: parseFloat(g.commission),
                    hiddenAmount: g.coupon_price,
                    couponInfo: '优惠券满'+ g.coupon_condition + '可用！',
                    couponAmount: g.coupon_price,
                    hiddenInfo: '优惠券满'+ g.coupon_condition + '可用！',
                    userType: g.is_tmall,
                    hiddenActivityId: g.coupon_id,
                    hiddenStartTime: g.coupon_start_time,
                    hiddenEndTime: g.coupon_end_time,
                    hiddenStartFee: g.coupon_condition,
                    tkRate: g.commission,
                    adTitle: g.goods_introduce,
                    isCheap: (parseFloat(g.goods_price) - parseInt(g.coupon_price)) < 10 ? 1 : 0,
                    isHot: parseInt(g.goods_sales) > 5000 ? 1 : 0,
                    tag: qtkTag[g.goods_cat],
                    tkType: commissionType[g.commission_type],
                    goodShortTitle:g.goods_short_title,
                    extActivityId: g.coupon_id
                }
                goods.push(good);
            });
            var arr = {};
            arr.auctionIdList = auctionIds;
            goodsDao.isExist(arr, function(err, data) {
                if(err) {
                    console.log('采集轻淘客后，检查重复商品失败！');
                    return;
                }
                goods = commonUtil.duplicateRemoval(data, goods);
                do{
                    if(goods.length > 40){
                        saveGatherGoods(goods.splice(0, 40), '轻淘客', onSaveGoods);
                    }
                    else{
                        saveGatherGoods(goods.splice(0, goods.length), '轻淘客', onSaveGoods);
                    }
                }while(goods.length > 0);
                if(goods.length == 0) {
                    ++page;
                    setTimeout(function(){
                        console.log(page);
                        spiderQtkGoods(page);
                    }, 4000);
                }
            });
        }
        catch(e) {

        }
    });
}

spiderGoodsDao.spiderDtkGoods = function(timely) {
    spiderDtkGoods(1, timely);
}

spiderGoodsDao.spiderHdkGoods = function() {
    spiderHdkGoods(1);
}

spiderGoodsDao.spiderMtkGoods = function() {
    spiderMtkGoods(1);
}

spiderGoodsDao.spiderYsdGoods = function() {
    spiderYsdGoods(1);
}

spiderGoodsDao.spiderTkjdGoods = function() {
    spiderTkjdGoods(1);
}

spiderGoodsDao.spiderQtkGoods = function() {
    spiderQtkGoods(1);
}

function doUpdateActivityType(auctionIdList, data) {
    DBFactory.getConnection(function(error, connection){
        if(error) {
            console.log(error);
            return;
        } else {
            var sql = "update goods set activityType = ? where auctionId in ( ? ) ";
            connection.query(sql, [data.type, auctionIdList], function(err, result) {
                if(err) {
                    console.log('doUpdateActivityType err: ' + err);
                    connection.release();
                } else {
                    connection.release();
                    if(auctionIdList.length == 100) {
                        ++data.page;
                        console.log(data.page);
                        console.log(result.affectedRows);
                        setTimeout(function() {
                            spiderGoodsDao.spiderActivityType(data);
                        }, 1000);
                    } else {
                        return;
                    }
                }
            });
        }
    });
}
spiderGoodsDao.spiderActivityType = function(data) {
    if(!data || !data.page || !data.type) return;
    if(data.type == '聚划算') {
        var url = 'http://www.dataoke.com/qlist/?px=zh&tqg_jhs=calculate&page=' + data.page;
    } else if(data.type == '淘抢购'){
        var url = 'http://www.dataoke.com/qlist/?px=zh&tqg_jhs=rush&page=' + data.page;
    }
    request({url: url, method: 'GET'}, function(error, response, body) {
        if(error || !response || response.statusCode != 200 && !body) {
            console.log(error);
            console.log('页面访问失败！');
        } else {
            var $ = cheerio.load(body);
            var goodsList = $('.goods-item');
            var auctionIdList = [];
            for(var i=0; i<goodsList.length; i++) {
                auctionIdList.push(goodsList.eq(i).attr('data_goodsid'));
            }
            if(auctionIdList.length > 0) doUpdateActivityType(auctionIdList, data);
            return;
        }
    });
}